#ifndef MANAGERBERTHIER_H_
#define MANAGERBERTHIER_H_

#include <map>
#include <string>
#include <queue>
#include <list>
#include <ctime>
#include <vector>

#include <boost/tr1/memory.hpp>
#include <boost/thread/mutex.hpp>

#include "../classes_net/URLFileDealer.h"
#include "../classes_net/Dominio.h"
#include "../classes_net/URL.h"

#include "../classes_outros/DownloaderQueueException.h"

class URLComparison
{
public:
  bool operator() (const URL * lhs, const URL * rhs) const
  {
    return ( int( lhs->getDepth() ) > int( rhs->getDepth() ) );
  }
};

class ManagerBerthier {
private:
    ManagerBerthier();
    bool refillQueueBuffer();
    std::string getRobots(std::string);
	URL * findURL();
    
public:
    URL * getNewURL(int) throw(DownloaderQueueException);
    static std::tr1::shared_ptr<ManagerBerthier> getInstance() {return theManagerBerthier;}
    void addURLs(std::list<URL>);
    virtual ~ManagerBerthier()
    { 
//    	  std::map<unsigned int, Dominio *>::iterator it;
//    	  std::map<unsigned long long, Dominio *>::iterator it;
    	std::map<std::string, Dominio *>::iterator it;
    	for ( it=domainHashMap.begin() ; it != domainHashMap.end(); it++ )
    		delete it->second;
    }
    //void testPriorityQueue();
    void initializeDownloaderQueues (const int) throw(DownloaderQueueException);
    bool refillQueuesOnce() throw(DownloaderQueueException);
    
private:
    static std::tr1::shared_ptr<ManagerBerthier> theManagerBerthier;
    const int timeBetweenVisits;
	std::queue<URL *> urlQueueBuffer;
//	std::map<unsigned int, Dominio *> domainHashMap;
//	std::map<unsigned long long int, Dominio *> domainHashMap;	
	std::map<std::string, Dominio *> domainHashMap;	
	std::vector< std::queue<URL *> > urlQueueVector;
	boost::mutex urlQueueBufferMutex;
	boost::mutex domainHashMapMutex;
	boost::mutex * downloaderQueueMutex;
	bool queuesInitialized;
	int numQueues;
	static const unsigned int URLS_PER_QUEUE;
};



#endif /*MANAGERBERTHIER_H_*/
